use crate::domain::login::service;
use crate::extends::response;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
use tracing::info;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LoginBody {
    pub userName: String,
    pub passwordMd5: String,
}

//登录验证
#[fn_handler]
pub async fn login(req: &mut Request, _depot: &mut Depot, res: &mut Response) {
    let loginBody = req.read::<LoginBody>().await.unwrap();

    info!("userName={}", &loginBody.userName);

    let token = service::login(loginBody.userName, loginBody.passwordMd5).await;

    info!("token={}", &token);

    if token == "" {
        res.render_json(&response::fail_result("登录失败".to_owned()));
    } else {
        res.render_json(&response::success_result(token.to_owned()));
    }
}

//登出
#[fn_handler]
pub async fn logout(req: &mut Request, _depot: &mut Depot, res: &mut Response) {
    res.render_json(&response::success_result("".to_owned()));
}
